package com.itheima.bos.web.action.transit;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.transit.TransitInfo;
import com.itheima.bos.service.transit.TransitInfoService;
import com.itheima.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/*
 * 运单配送运输流程
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class TransitInfoAction extends BaseAction<TransitInfo> {

	@Autowired
	private TransitInfoService transitInfoService;

	/*
	 * 创建并保存transitInfo方法
	 */
	private String wayBillIds;

	public void setWayBillIds(String wayBillIds) {
		this.wayBillIds = wayBillIds;
	}

	@Action(value = "transit_creat", results = { @Result(name = "success", type = "json") })
	public String create() {

		Map<String, Object> result = new HashMap<>();
		try {
			// 创建成功
			transitInfoService.createTransits(wayBillIds);
			result.put("success", true);
			result.put("msg", "开启中转配送成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "开启中转配送失败");
		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	/*
	 * 分页查询运输管理
	 */
	@Action(value = "transit_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		
		Pageable pageable = new PageRequest(page-1, rows);
		Page<TransitInfo> pageData = transitInfoService.findPageData(pageable);
		
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
}
